<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row :gutter="30">
        <el-dialog title="修改订单信息" :visible.sync="editDialogVisible" width="50%" @close="editCloseDia">
        <el-form :model="editForm" :rules="addFormRules" ref="editFormRef" label-width="70px">
          <el-form-item label="餐码" prop="canma">
            <el-input v-model="editForm.canma" disabled></el-input>
          </el-form-item>
          <el-form-item label="金额" prop="price">
            <el-input v-model="editForm.canma" disabled></el-input>
          </el-form-item>
          <el-form-item label="备注" prop="beizhu">
            <el-input v-model="editForm.beizhu"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="editDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="editUserInfo">确 定</el-button>
        </span>
      </el-dialog>
        <el-table :data="dingdans" border stripe>
        <el-table-column label="#" type="index" ></el-table-column>
        <el-table-column label="生成时间" prop="date"></el-table-column>
        <el-table-column label="取餐码" prop="canma"></el-table-column>
        <el-table-column label="订单详情" prop="caipinxx"></el-table-column>
        <el-table-column label="总计（元）" prop="price"></el-table-column>
        <el-table-column label="备注" prop="beizhu"></el-table-column>
        <el-table-column label="操作" align="right">
          <template slot-scope="scope">
            <el-tooltip content="修改信息" placement="top" effect="light" :enterable="false">
              <el-button
                size="mini"
                icon="el-icon-edit"
                @click="handleEdit(scope.row.dingdan_id)"
                type="primary"
              ></el-button>
            </el-tooltip>
            <el-tooltip content="删除信息" placement="top" effect="light" :enterable="false">
              <el-button
                size="mini"
                icon="el-icon-delete"
                @click="handleDelete(scope.row.dingdan_id)"
                type="danger"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      </el-row>
      <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.currentPage"
      :page-sizes="[5, 10, 15, 30]"
      :page-size="queryInfo.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total=queryInfo.total>
    </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryInfo: {
        currentPage: 1,
        // 当前每页显示多少条数据
        pageSize: 5,
        total: 0
      },
      dingdans: [],
      editDialogVisible: false,
      editForm: [],
      addFormRules: {}
    }
  },
  methods: {
    async fetch() {
      const { data: res } = await this.$http.get('/dingdan', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取订单列表失败！')
      }
      for (const i in res.dingdanxx) {
        if (res.dingdanxx[i].caipinxx != null) {
          const caipin = JSON.parse(res.dingdanxx[i].caipinxx)
          let str = ''
          for (const j in caipin) {
            str += caipin[j].cp_name + '（份数x' + caipin[j].num + '）|| '
          }
          res.dingdanxx[i].caipinxx = str
        }
      }
      this.dingdans = res.dingdanxx
      this.queryInfo = res.pagination
    },
    handleSizeChange(val) {
      this.queryInfo.pageSize = val
      this.fetch()
    },
    async handleCurrentChange(val) {
      this.queryInfo.currentPage = val
      this.fetch()
    },
    async handleEdit(id) {
      const { data: res } = await this.$http.get(`/dingdan${id}`)
      if (res.meta.status !== 200) {
        return this.$message.error('查询订单信息失败')
      }
      this.editForm = res.usersedit
      this.editDialogVisible = true
    },
    editUserInfo() {
      this.$refs.editFormRef.validate(async valid => {
        // console.log(valid)
        if (!valid) return
        const { data: res } = await this.$http.put('/dingdan' + this.editForm.dingdan_id, this.editForm)
        if (res.meta.status !== 200) {
          return this.$message.error('更新订单信息失败')
        }
        this.$message.success('更新订单信息成功')
        this.editDialogVisible = false
        this.fetch()
      })
    },
    handleDelete(id) {
      this.$confirm('此操作将永久删除订单信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$http.delete(`dingdan${id}`).then(res => {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.fetch()
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    editCloseDia() {
      this.$refs.editFormRef.resetFields()
    }
  },
  created () {
    this.fetch()
  }
}
</script>

<style lang="less" scoped>

</style>
